const axios = require('axios')
// const Vue = require('vue')
const { Message } = require('element-ui');
const router = require('../../router/index');

module.exports = {
    namespaced: true,
    state: {
        ordersData: [],
        ordersLoseData: [],
        ordersshippedData: [],
        ordersdelayedData: [],
        form: {
            _id: '',
            num: '',
            address: '',
            price: ''
        },
        dialogFormVisible: false,
        hide: true,
        addOrdersData: {
            name: '',
            num: '',
            price: '',
            state: '',
            imagePath: '',
            shopsId:''
        },
        pageData: {
            pageSize: 3,
            currentPage: 1,
            pages: 0,
            total: 0,
            rows: []
        },
        pageData2: {
            pageSize: 3,
            currentPage: 1,
            pages: 0,
            total: 0,
            rows: []
        },
        pageData3: {
            pageSize: 3,
            currentPage: 1,
            pages: 0,
            total: 0,
            rows: []
        },
        pageData4: {
            pageSize: 3,
            currentPage: 1,
            pages: 0,
            total: 0,
            rows: []
        },
        shopsData:[]

    },
    mutations: {
        getOrders(state, data) {
            // if(data.type==success){
            state.ordersData = data
        },
        getOrdersLose(state, data) {
            state.ordersLoseData = data
        },
        getOrdersshipped(state, data) {
            state.ordersshippedData = data
        },
        getOrdersdelayed(state, data) {
            state.ordersdelayedData = data
        },
        updateOrders(state, row) {
            state.form = { ...row }
            state.dialogFormVisible = true
        },
        close(state, row) {
            state.dialogFormVisible = false
        },
        getShops(state,data){
            console.info(data)

            state.shopsData=data
        }


    },
    actions: {
        //分页
        handleSizeChange1(context, val) {
            context.state.pageData.pageSize = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
        handleCurrentChange1(context, val) {
            context.state.pageData.currentPage = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
        handleSizeChange2(context, val) {
            context.state.pageData2.pageSize = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
        handleCurrentChange2(context, val) {
            context.state.pageData2.currentPage = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },

        handleSizeChange3(context, val) {
            context.state.pageData3.pageSize = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
        handleCurrentChange3(context, val) {
            context.state.pageData3.currentPage = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },

        handleSizeChange4(context, val) {
            context.state.pageData4.pageSize = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
        handleCurrentChange4(context, val) {
            context.state.pageData4.currentPage = val
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')
            context.dispatch('getOrdersshippedAsync')
        },
      
        async getShopsAsync(context){
            const {data}=await axios.get('/shops/getShops')
            // console.info(data.name)
            context.commit('getShops', data.rows)

        },
        //获取有效订单
        async getOrdersAsync(context) {
            const { data } = await axios.get('/orders/getOrders', {
                params: {
                    state: '有效',
                    pageSize: context.state.pageData.pageSize,
                    currentPage: context.state.pageData.currentPage

                }
            })
            context.state.pageData.total = data.total

            context.commit('getOrders', data.rows)

        },
        //失效订单
        async getOrdersLoseAsync(context) {
            const { data } = await axios.get('/orders/getOrders', {
                params: {
                    state: '失效',
                    pageSize: context.state.pageData2.pageSize,
                    currentPage: context.state.pageData2.currentPage

                }
            })
            context.state.pageData2.total = data.total
            context.commit('getOrdersLose', data.rows)

        },
        //已发货商品
        async getOrdersshippedAsync(context) {
            const { data } = await axios.get('/orders/getOrders', {
                params: {
                    state: '发货',
                    pageSize: context.state.pageData3.pageSize,
                    currentPage: context.state.pageData3.currentPage

                }
            })
            context.state.pageData3.total = data.total
            context.commit('getOrdersshipped', data.rows)
        },
        //延长收货
        async getOrdersdelayedAsync(context) {

            const { data } = await axios.get('/orders/getOrders', {
                params: {
                    state: '延发',
                    pageSize: context.state.pageData4.pageSize,
                    currentPage: context.state.pageData4.currentPage

                }
            })
            context.state.pageData4.total = data.total
            context.commit('getOrdersdelayed', data.rows)
        },

        //修改订单
        async updateOrdersAsync(context, _id) {
            const { data } = await axios.post('/orders/updateOrders', context.state.form)
            context.state.dialogFormVisible = false
            context.dispatch('getOrdersAsync')
        },
        //取消订单
        async deletOrdersAsync(context, order) {
            if (order.state == '失效') {
                order.state = '有效'
            } else if (order.state == '有效') {
                order.state = '失效'
            } else if (order.state == '延发') {
                order.state = '失效'
            }
            console.info(order.state)
            const { data } = await axios.post('/orders/updateOrders', order)
            if(data&&order.state == '失效'){
                Message({
                    type: "success",
                    message: "订单已取消"

                });
                context.dispatch('getOrdersLoseAsync')
                context.dispatch('getOrdersAsync')
                context.dispatch('getOrdersdelayedAsync') 
            }else if(data&&order.state == '有效'){
                Message({
                    type: "success",
                    message: "成功购买该商品"
                });
                context.dispatch('getOrdersLoseAsync')
                context.dispatch('getOrdersAsync')
                context.dispatch('getOrdersdelayedAsync') 
            }
                 

        },
        async shippedOrdersAsync(context, order) {
            order.state = '发货'

            console.info(order.state)
            const { data } = await axios.post('/orders/updateOrders', order)
            if(data){
                Message({
                    type: "success",
                    message: "成功发货"
                });
                context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersshippedAsync')
            context.dispatch('getOrdersdelayedAsync')
            }else{
                message.err('发货失败！')
            }
            


        },
        async delayedOrdersAsync(context, order) {
            order.state = '延发'
            const { data } = await axios.post('/orders/updateOrders', order)
            if(data){
                Message({
                    type: "success",
                    message: "商品延期七天后发出"
                });
                context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')   
            }else{
                message.err('商品已过延期最大限度，请选择发货或联系买家退货！')
            }
                  
        },
        async addOrdersAsync(context) {
            const { data } = await axios.post('/orders/addOrders', context.state.addOrdersData)
            return data
        }

    }
}